﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace TienLen
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // As you can see here, we don't call Application.Run(frm) as the main menu form
            // will exit at times, depending on what the user has chosen to do.
            new MainMenu().Show();

            #region Preload Game Assemblies

            string gameDir = Application.StartupPath + "\\assem\\games";
            string[] games = Directory.GetFiles(gameDir, "*.dll");

            for (int i = 0; i < games.Length; i++)
            {
                try
                {
                    Assembly assem = Assembly.LoadFile(games[i]);

                    foreach (Type type in assem.GetTypes())
                    {
                        Type[] interfaces = type.GetInterfaces();

                        if (interfaces.Length == 2 &&
                            interfaces[0] == typeof(CGLib.IGame) && interfaces[1] == typeof(IDisposable))
                        {
                            CGLib.IGame game = (CGLib.IGame)Activator.CreateInstance(type);

                            SvrFunc.Games.Add(game);
                            SvrFunc.GameGUID.Add(type.GUID);
                        }
                    }
                }
                catch (Exception)
                {
                    continue;
                }
            }

            #endregion

            Application.Run();
        }
    }
}
